<?php

namespace App\Admin\Forms;


use App\Models\Posts\WxPost;
use App\Models\WxCircle;
use App\Models\WxNotice;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;


class PostsForms extends Form implements LazyRenderable
{
    use LazyWidget;


    /**
     * 主题颜色.
     *
     * @var array
     */
    protected $status = [
        1 => '审核通过',
        2 => '驳回',
    ];

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        $id = $input['id'];
        $status = $input['is_examine'];
        $overrule_content = $input['overrule_content'];
        WxPost::where('id', $id)->update([
            'is_examine' => $status
        ]);

        $uid = WxPost::where('id', $id)->value('user_id');
        if ($status == 1) {
            // 发送通知
            (new WxNotice())->addNotice($uid, '帖子审核通知', '您的发表的帖子已经审核通过啦', 0);
        } elseif ($status == 2) {
            // 发送通知
            (new WxNotice())->addNotice($uid, '帖子审核通知', '您的发表的帖子被驳回了，驳回原因：' . $overrule_content, 0);
        }

        return $this->response()
            ->success('操作成功')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $this->hidden('id')->value($this->payload["id"]);
//        $this->radio('is_examine', "状态")->options($this->status)->default(1);
        $this->radio('is_examine', '状态')
            ->when(1, function (Form $form) {

            })
            ->when(2, function (Form $form) {
                $form->textarea('overrule_content', "驳回原因");
            })
            ->options([
                1 => '通过',
                2 => '驳回',

            ])
            ->default(1);
    }

}
